package me.boot.common;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import me.boot.common.constant.Const;
import me.boot.common.context.ContextUtil;
import me.boot.common.data.model.AppMode;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;

@Slf4j
@AutoConfigureOrder(-100)
@Configurable
public class BootConfiguration {

    @PostConstruct
    void init() {
        log.info("Init BootConfiguration");

        Environment env = SpringUtil.getApplicationContext().getEnvironment();
        String discoveryServerAddr = env.getProperty(Const.CLOUD_SERVER_KEY);
        //定义：如果有配置discoveryServer，即为微服务应用
        boolean isCloud = StrUtil.isNotBlank(discoveryServerAddr);
        AppMode mode = isCloud ? AppMode.Cloud : AppMode.Mono;
        ContextUtil.setAppMode(mode);

        log.info("应用形态: {}", mode);
    }
}
